package com.gemme.gemme;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.gemme.gemme.fabu.FabuFragment;
import com.gemme.gemme.fragment.MyFragmentPagerAdapter;
import com.gemme.gemme.gwc.GwcNewFragment;
import com.gemme.gemme.home.Home2Fragment;
import com.gemme.gemme.shequ.ShequFragment;
import com.gemme.gemme.util.ActivityCollectorUtil;
import com.gemme.gemme.view.FaBuDialog;
import com.gemme.gemme.view.NoScrollViewPager;
import com.gemme.gemme.wode.Wode2Fragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener, RadioGroup.OnCheckedChangeListener {

    private static MainActivity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        activity = this;

        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(Color.parseColor("#000000"));
        }

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        ActivityCollectorUtil.addActivity(this);
    }
    List<Fragment> fmList;
    NoScrollViewPager mVp_main;
    RadioGroup  rg_main;
    RadioButton rb_home;
    RadioButton rb_shequ;
    RadioButton rb_fabu;
    RadioButton rb_gwc;
    RadioButton rb_wode;
    private void initView() {
        fmList = new ArrayList<>();
        fmList.add(new Home2Fragment());
//        fmList.add(new ShequFragment());
//        fmList.add(new FabuFragment());
//        fmList.add(new GwcNewFragment());
//        fmList.add(new Wode2Fragment());


        rg_main = findViewById(R.id.rg_main);

        rb_home = findViewById(R.id.rb_home);

        rb_shequ = findViewById(R.id.rb_shequ);

        rb_fabu = findViewById(R.id.rb_fabu);

        rb_gwc = findViewById(R.id.rb_gwc);

        rb_wode = findViewById(R.id.rb_wode);
        mVp_main = findViewById(R.id.vp_main);
        mVp_main.setOffscreenPageLimit(fmList.size());
        mVp_main.setScanScroll(true); // 设置ViewPager不可滑动
        mVp_main.addOnPageChangeListener(this); // ViewPager滑动监听
        mVp_main.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fmList));

       /* rg_main.setOnClickListener(this);
        rb_home.setOnClickListener(this);
        rb_shequ.setOnClickListener(this);
        rb_fabu.setOnClickListener(this);
        rb_gwc.setOnClickListener(this);*/
        rg_main.setOnCheckedChangeListener(this); // RadioGroup点击监听

        int id = getIntent().getIntExtra("id", 0);
        if (id == 2) {
            rg_main.check(R.id.rb_shequ);
        }

        //生日弹窗
//        new BirthDayDialog().showDialog(mContext);
    }

    public static MainActivity getTabUI() {
        return activity;
    }

    public void setTab(int index) {
        switch (index) {
            case 0: //首页
                rg_main.check(R.id.rb_home);
                break;

            case 1: //社区
                rg_main.check(R.id.rb_shequ);
                break;

            case 2: //购物车
                rg_main.check(R.id.rb_gwc);
                break;

            case 4: //我的
                rg_main.check(R.id.rb_wode);
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int i) {
        switch (i) {
            case 0:
                rb_home.setChecked(true);
                break;
            case 1:
                rb_shequ.setChecked(true);
                break;
            case 2:
                rb_fabu.setChecked(true);
                break;
            case 3:
                rb_gwc.setChecked(true);
                break;
            case 4:
                rb_wode.setChecked(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i) {
            case R.id.rb_home:
                fmList.clear();
                fmList.add(new Home2Fragment());
                mVp_main.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fmList));
                mVp_main.setCurrentItem(0, false);
                break;

            case R.id.rb_shequ:
                fmList.clear();
                fmList.add(new Home2Fragment());
                fmList.add(new ShequFragment());
                mVp_main.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fmList));
                mVp_main.setCurrentItem(1, false);
                break;

            case R.id.rb_fabu:
                fmList.clear();
                fmList.add(new Home2Fragment());
                fmList.add(new ShequFragment());
                fmList.add(new FabuFragment());
                fmList.add(new GwcNewFragment());
                fmList.add(new Wode2Fragment());
                mVp_main.setCurrentItem(2, false);
                new FaBuDialog().showDialog(this);
                break;

            case R.id.rb_gwc:
                fmList.clear();
                fmList.add(new Home2Fragment());
                fmList.add(new ShequFragment());
                fmList.add(new FabuFragment());
                fmList.add(new GwcNewFragment());
                fmList.add(new Wode2Fragment());
                mVp_main.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fmList));
                mVp_main.setCurrentItem(3, false);
                break;

            case R.id.rb_wode:
                fmList.clear();
                fmList.add(new Home2Fragment());
                fmList.add(new ShequFragment());
                fmList.add(new FabuFragment());
                fmList.add(new GwcNewFragment());
                fmList.add(new Wode2Fragment());
                mVp_main.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fmList));
                mVp_main.setCurrentItem(4, false);
                break;
        }
    }

    public ShequFragment getFabuFragment(){
        ShequFragment fabuFragment = null;
        try {
            fabuFragment = (ShequFragment)(fmList.get(1));
        }catch (Exception e){

        }
        return fabuFragment;

    }

    @Override
    protected void onResume() {
        super.onResume();
        int checkedRadioButtonId = rg_main.getCheckedRadioButtonId();
//        R.id.rb_fabu
        if(checkedRadioButtonId == R.id.rb_fabu){ //todo 跳转社区详情页--广场页
            mVp_main.setCurrentItem(0, false);
            getFabuFragment().toGuangchang();
        }
        System.out.println("");
    }

    public void toOnResume(){
        onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollectorUtil.removeActivity(this);
    }
}